$(function () {
    //全局定义页数与总条数
    var markid = getUrlString('markid');
    var page = -1,totalSize = 0,sort= 0;


    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                $('#myTitleList').empty();
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                getDate(page)
            }
        }
    });

    function getDate(page) {
        var params = {
            'markid':markid,
            'page':page
        };
        getData('get', '/niu/apiv3/weibo_chenghaolist.php', params, function (data) {
            console.log(data);
            var append = '';
            if(data.pagetotal === 0){
                mRefresh.endUpLoading(true);
                $('.minirefresh-hardware-speedup').hide();
                $('.nullContent').show();
                return
            }else {
                $('.nullContent').hide();
                $('.minirefresh-hardware-speedup').show();
            }
            $(data.data).each(function () {
                append += '<li>' +
                    '                    <p><img src="https://api.odancool.com'+this.icon+'" alt=""></p>' +
                    '                    <p>'+this.intro+'</p>' +
                    '                    <p>' +
                    '                        <span>获得时间：'+subTime(this.starttime)+'</span>' +
                    '                        <span>到期时间：'+subTime(this.endtime)+'</span>' +
                    '                    </p>' ;
                    if(this.set === '0'){
                        append += '                    <div class="ZBBtn">装备</div>' ;
                    }else if(this.set === '1'){
                        append += '<div class="XXBtn">卸下</div>'
                    }
                    append += '<input type="hidden" class="id" value="'+this.id+'"> ' +
                        '               </li>'
            });
            $('#myTitleList').append(append);

            $('#myTitleList li .ZBBtn').click(function () {
                var id = $(this).next('.id').val();
                var params = {
                    'markid':markid,
                    'chid':id,
                    'equip':'1'
                };
                getData('get', '/niu/apiv3/weibo_chenghaochange.php', params, function (data) {
                    console.log(data);
                    if(data.resultcode === '0'){
                        laymsg('装备成功');
                        setTimeout(function () {
                            window.location.reload()
                        },500)
                    }
                })
            });

            $('#myTitleList li .XXBtn').click(function () {
                var id = $(this).next('.id').val();
                var params = {
                    'markid':markid,
                    'chid':id,
                    'equip':'0'
                };
                getData('get', '/niu/apiv3/weibo_chenghaochange.php', params, function (data) {
                    console.log(data);
                    if(data.resultcode === '0'){
                        laymsg('卸下成功');
                        setTimeout(function () {
                            window.location.reload()
                        },500)
                    }
                })
            });


            totalSize = totalSize + 10;
            if (totalSize > data.datacount)
                mRefresh.endUpLoading(true);
            else{
                mRefresh.endUpLoading(false);
            }
        })
    }

});